import { Inject, Injectable } from '@nestjs/common';
import { Cron, CronExpression } from '@nestjs/schedule';
import mysqldump from 'mysqldump';
// import configuration from '../../config/index'; // 不同环境的配置文件
import { ConfigModule, ConfigService } from '@nestjs/config';

@Injectable()
export class SqlTaskService {
  @Inject(ConfigService)
  private configService: ConfigService;
  // 每天凌晨一点备份数据库，CronExpression.EVERY_DAY_AT_1AM
  // 每10秒，EVERY_10_SECONDS
  @Cron('10 * * * * *')
  handleCron() {
    console.log('定时任务')
    // const sqlInfo = configuration().db.mysql
    mysqldump({
      connection: {
          host: this.configService.get('DB_HOST'), // 数据库地址
          user: this.configService.get('DB_USERNAME'), // 数据库帐号
          password: this.configService.get('DB_PASSWORD'), // 数据库密码
          database: this.configService.get('DB_BATABASE') // 数据库表名
      },
      dumpToFile: `../../../backSql/sql.sql`, // 存入的文件目录
    });
    console.log('备份数据库');
  }
}

